{
 "cells": [
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-08T07:17:00.417084Z",
     "start_time": "2025-05-08T07:16:57.653970Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#随机初始化权重\n",
    "import numpy as np\n",
    "\n",
    "# 定义激活函数和其导数\n",
    "def sigmoid(x):\n",
    "    return 1 / (1 + np.exp(-x))\n",
    "\n",
    "def sigmoid_derivative(x):\n",
    "    return x * (1 - x)\n",
    "\n",
    "# 定义训练数据（异或问题）\n",
    "inputs = np.array([[0,0],[0,1],[1,0],[1,1]])\n",
    "expected_output = np.array([[0],[1],[1],[0]])\n",
    "\n",
    "# 网络参数\n",
    "input_size = 2\n",
    "hidden_size = 3\n",
    "output_size = 1\n",
    "learning_rate = 0.1\n",
    "epochs = 10000\n",
    "num_init = 10  # 尝试不同的随机初始化次数\n",
    "\n",
    "# 存储最佳模型\n",
    "best_loss = float('inf')\n",
    "best_weights = None\n",
    "best_biases = None\n",
    "\n",
    "for i in range(num_init):\n",
    "    # 随机初始化权重和偏置\n",
    "    weights_input_hidden = np.random.rand(input_size, hidden_size)\n",
    "    biases_hidden = np.random.rand(hidden_size)\n",
    "    weights_hidden_output = np.random.rand(hidden_size, output_size)\n",
    "    biases_output = np.random.rand(output_size)\n",
    "\n",
    "    for epoch in range(epochs):\n",
    "        # 前向传播\n",
    "        hidden_layer_activation = sigmoid(np.dot(inputs, weights_input_hidden) + biases_hidden)\n",
    "        predictions = sigmoid(np.dot(hidden_layer_activation, weights_hidden_output) + biases_output)\n",
    "\n",
    "        # 计算误差\n",
    "        error = expected_output - predictions\n",
    "        loss = np.mean(np.square(error))\n",
    "\n",
    "        # 反向传播\n",
    "        d_output = error * sigmoid_derivative(predictions)\n",
    "        error_hidden_layer = d_output.dot(weights_hidden_output.T)\n",
    "        d_hidden_layer = error_hidden_layer * sigmoid_derivative(hidden_layer_activation)\n",
    "\n",
    "        # 更新权重和偏置\n",
    "        weights_hidden_output += hidden_layer_activation.T.dot(d_output) * learning_rate\n",
    "        biases_output += np.sum(d_output, axis=0) * learning_rate\n",
    "        weights_input_hidden += inputs.T.dot(d_hidden_layer) * learning_rate\n",
    "        biases_hidden += np.sum(d_hidden_layer, axis=0) * learning_rate\n",
    "\n",
    "    # 保存最小损失对应的权重和偏置\n",
    "    if loss < best_loss:\n",
    "        best_loss = loss\n",
    "        best_weights = (weights_input_hidden, weights_hidden_output)\n",
    "        best_biases = (biases_hidden, biases_output)\n",
    "\n",
    "# 使用最佳模型进行预测\n",
    "weights_input_hidden, weights_hidden_output = best_weights\n",
    "biases_hidden, biases_output = best_biases\n",
    "hidden_layer_activation = sigmoid(np.dot(inputs, weights_input_hidden) + biases_hidden)\n",
    "predictions = sigmoid(np.dot(hidden_layer_activation, weights_hidden_output) + biases_output)\n",
    "\n",
    "print(\"Final predictions:\")\n",
    "print(predictions)\n",
    "\n"
   ],
   "id": "f9edc28a6869fafe",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Final predictions:\n",
      "[[0.05461413]\n",
      " [0.94973818]\n",
      " [0.94972971]\n",
      " [0.05362768]]\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-08T07:17:00.583126Z",
     "start_time": "2025-05-08T07:17:00.430443Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#动量\n",
    "# 动量参数\n",
    "momentum = 0.9\n",
    "velocity_w_input_hidden = np.zeros_like(weights_input_hidden)\n",
    "velocity_b_hidden = np.zeros_like(biases_hidden)\n",
    "velocity_w_hidden_output = np.zeros_like(weights_hidden_output)\n",
    "velocity_b_output = np.zeros_like(biases_output)\n",
    "\n",
    "for epoch in range(epochs):\n",
    "    # 前向传播、计算误差、反向传播的代码（与上面相同）\n",
    "\n",
    "    # 更新权重和偏置时加入动量\n",
    "    velocity_w_hidden_output = momentum * velocity_w_hidden_output + learning_rate * hidden_layer_activation.T.dot(d_output)\n",
    "    velocity_b_output = momentum * velocity_b_output + learning_rate * np.sum(d_output, axis=0)\n",
    "    velocity_w_input_hidden = momentum * velocity_w_input_hidden + learning_rate * inputs.T.dot(d_hidden_layer)\n",
    "    velocity_b_hidden = momentum * velocity_b_hidden + learning_rate * np.sum(d_hidden_layer, axis=0)\n",
    "\n",
    "    weights_hidden_output += velocity_w_hidden_output\n",
    "    biases_output += velocity_b_output\n",
    "    weights_input_hidden += velocity_w_input_hidden\n",
    "    biases_hidden += velocity_b_hidden\n"
   ],
   "id": "77accfb38aa7a37c",
   "outputs": [],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-08T07:17:00.594164Z",
     "start_time": "2025-05-08T07:17:00.588850Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#使用优化器\n",
    "import tensorflow as tf\n",
    "\n",
    "# 假设inputs是输入数据，weights是权重\n",
    "inputs = tf.constant([[0.0, 0.0], [0.0, 1.0], [1.0, 0.0], [1.0, 1.0]], dtype=tf.float32)\n",
    "weights = tf.random.normal([2, 3], dtype=tf.float32)  # 假设权重是2x3的矩阵\n",
    "\n",
    "# 确保inputs和weights的数据类型一致\n",
    "inputs = tf.cast(inputs, tf.float32)\n",
    "weights = tf.cast(weights, tf.float32)\n",
    "\n",
    "# 执行矩阵乘法\n",
    "hiddenlayeractivation = tf.matmul(inputs, weights)\n",
    "\n",
    "print(hiddenlayeractivation)\n",
    "\n"
   ],
   "id": "aa0b0930cccd3bb3",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tf.Tensor(\n",
      "[[ 0.         0.         0.       ]\n",
      " [ 1.7745123  1.0953304 -1.9704698]\n",
      " [-3.2716508 -1.5692956 -1.4103999]\n",
      " [-1.4971385 -0.4739653 -3.3808699]], shape=(4, 3), dtype=float32)\n"
     ]
    }
   ],
   "execution_count": 9
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
